package com.yc.ui.admin;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.ui.Order;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class GoodslistDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Text text_l_id;
	private Text text_c_name;
	private Text text_r_id;
	private Text text_r_type;
	private Text text_start_date;
	private Text text_end_date;
	private Text text_r_price;
	private Text text_day;
	private Text text_totalprice;
	private Text text_l_price;
	private Text text_f_price;
	private byte[] bt;//图片的字节数组
	GoodsIDao dao = new GoodsDao();
	String l_id;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public GoodslistDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */	
	public Object open(Table table1) {
		createContents(table1);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents(Table table1) {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(779, 700);
		shell.setText("消费详细记录");
		SWTUtil.centerWindow(shell);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(69, 318, 677, 174);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		shell.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约1.PNG") );
		SWTUtil.imagetSize(shell);				
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(172);
		tblclmnNewColumn.setText("商品名称");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(103);
		tblclmnNewColumn_4.setText("单价");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(119);
		tblclmnNewColumn_1.setText("数量");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(133);
		tblclmnNewColumn_3.setText("总计");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(128);
		tblclmnNewColumn_2.setText("商品图片");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(45, 290, 76, 20);
		lblNewLabel.setText("其他消费：");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(135, 196, 76, 20);
		lblNewLabel_1.setText("房间号：");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		btnNewButton.setBounds(247, 606, 98, 30);
		btnNewButton.setText("打印账单");
		
		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnNewButton_1.setBounds(418, 606, 98, 30);
		btnNewButton_1.setText("取消");
		
		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setBounds(440, 525, 98, 20);
		lblNewLabel_3.setText("消费总计：");
		
		Label lblNewLabel_5 = new Label(shell, SWT.NONE);
		lblNewLabel_5.setText("房间类型：");
		lblNewLabel_5.setBounds(428, 89, 76, 20);
		
		Label lblNewLabel_7 = new Label(shell, SWT.NONE);
		lblNewLabel_7.setBounds(428, 140, 76, 20);
		lblNewLabel_7.setText("入住时间：");
		
		Label lblNewLabel_9 = new Label(shell, SWT.NONE);
		lblNewLabel_9.setBounds(135, 140, 76, 20);
		lblNewLabel_9.setText("顾客姓名：");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(135, 89, 76, 20);
		label.setText("住宿编号：");
		
		Label lblNewLabel_7_1 = new Label(shell, SWT.NONE);
		lblNewLabel_7_1.setText("退房时间：");
		lblNewLabel_7_1.setBounds(428, 196, 76, 20);
		
		Label lblNewLabel_4 = new Label(shell, SWT.NONE);
		lblNewLabel_4.setBounds(135, 261, 76, 20);
		lblNewLabel_4.setText("房间价格：");
		
		Label lblNewLabel_11 = new Label(shell, SWT.NONE);
		lblNewLabel_11.setBounds(428, 261, 76, 20);
		lblNewLabel_11.setText("住宿天湖：");
		
		text_l_id = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_l_id.setBounds(234, 89, 112, 26);
		
		text_c_name = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_c_name.setBounds(234, 140, 112, 26);
		
		text_r_id = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_r_id.setBounds(234, 193, 112, 26);
		
		text_r_type = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_r_type.setBounds(528, 89, 112, 26);
		
		text_start_date = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_start_date.setBounds(528, 137, 112, 26);
		
		text_end_date = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_end_date.setBounds(528, 193, 112, 26);
		
		text_r_price = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_r_price.setBounds(236, 258, 112, 26);
		
		text_day = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_day.setBounds(528, 258, 112, 26);
		
		text_totalprice = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_totalprice.setBounds(544, 522, 112, 26);
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setText("住宿消费：");
		lblNewLabel_2.setBounds(45, 224, 76, 20);
		
		Label lblNewLabel_4_1 = new Label(shell, SWT.NONE);
		lblNewLabel_4_1.setText("住宿消费总计：");
		lblNewLabel_4_1.setBounds(121, 501, 109, 20);
		
		text_l_price = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_l_price.setBounds(247, 498, 112, 26);
		
		Label lblNewLabel_4_1_1 = new Label(shell, SWT.NONE);
		lblNewLabel_4_1_1.setText("商品消费总计：");
		lblNewLabel_4_1_1.setBounds(126, 548, 104, 20);
		
		text_f_price = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		text_f_price.setBounds(247, 545, 112, 26);
		
		Label lblNewLabel_6 = new Label(shell, SWT.CENTER);
		lblNewLabel_6.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.BOLD));
		lblNewLabel_6.setBounds(165, 20, 385, 36);
		lblNewLabel_6.setText("消费详细记录");
		showValues(table1);
		showinfo(l_id);
	}
	
	public void showValues(Table table){		
		TableItem item = table.getItem(table.getSelectionIndex());
		l_id = item.getText(0);
		text_l_id.setText(l_id);
		text_c_name.setText(item.getText(1));
		text_r_id.setText(item.getText(2));
		text_r_type.setText(item.getText(3));
		text_start_date.setText(item.getText(4));
		text_end_date.setText(item.getText(5));
		text_l_price.setText(item.getText(6));
		text_f_price.setText(item.getText(7));
		text_totalprice.setText(item.getText(8));
	}
	
	private void showinfo(String l_id) {
		//查询信息‘
		List<Map<String, Object>> list = dao.findGoods(l_id);
		TableItem tableItem = null;
		//判空
		if(null == list || list.isEmpty()) {
			StringUtil.showMessageBox(shell, "你没有在南华客栈进行商品消费！！！");
			return;
		}
		//有信息  循环
		for(Map<String, Object> m : list) {
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(new String[] {
				StringUtil.toString(m.get("g_name")),
				StringUtil.toString(m.get("g_price")),
				StringUtil.toString(m.get("f_num")),
				StringUtil.toString(m.get("f_price"))
			});
			bt = ( byte[] )m.get("g_image");
			// 字节数组判空
			if( null != bt) {
				Image image = SWTUtil.imageSize(100, 50, new ByteArrayInputStream(bt));
				tableItem.setImage(4, image);
			}
		}
	}
}
